#region (c) 2009 Abdullin.com and Contributors

// Copyright (c) 2009 Abdullin.com and Contributors
// Site: http://abdullin.com/ncouch
// This code is released under the terms of the new BSD licence

#endregion

using NCouch.Error;
using NCouch.Model;

namespace NCouch
{
	public sealed class CouchServer : CouchAdapterBase
	{
		public CouchServer(CouchRestClient client) : base(client) {}

		public string[] GetDatabaseNames()
		{
			using (var response = Get("_all_dbs"))
			{
				Fail(response)
					.Unless(CouchStatus.Code200);
				return ReadObject<string[]>(response);
			}
		}

		public void CreateDatabase(string name)
		{
			using (var response = Put(name + "/"))
			{
				Fail(response)
					.On(CouchStatus.Code412, CouchError.DatabaseAlreadyExists)
					.Unless(CouchStatus.Code201);
			}
		}

		public ServerInfo GetInformation()
		{
			using (var response = Get(""))
			{
				Fail(response)
					.Unless(CouchStatus.Code200);

				return ReadObject<ServerInfo>(response);
			}
		}


		public void DeleteDatabase(string name)
		{
			using (var response = Delete(name + "/"))
			{
				Fail(response).Unless(CouchStatus.Code200);
			}
		}
	}
}